package com.aifeng.hassan_food.mine

import android.os.Bundle
import com.aifeng.base.BaseActivity
import com.aifeng.base.loadCircleImage
import com.aifeng.base.loadImage
import com.aifeng.common_ui.base.oneClick
import com.aifeng.common_ui.helper.getString
import com.aifeng.common_ui.helper.putShared
import com.aifeng.hassan_food.R
import com.aifeng.oss.OSSHelper
import com.aifeng.oss.scaleImage
import com.aifeng.utils.DialogHelper.showQRcodeDialog
import com.aifeng.utils.GlideEngine
import com.aifeng.utils.bindLifeCycle
import com.aifeng.utils.callback
import com.aifeng.utils.subscribeByNoError
import com.aifeng.viewmodel.SettingViewModel
import com.luck.picture.lib.PictureSelector
import com.luck.picture.lib.animators.AnimationType
import com.luck.picture.lib.config.PictureConfig
import com.luck.picture.lib.config.PictureMimeType
import com.luck.picture.lib.style.PictureWindowAnimationStyle
import kotlinx.android.synthetic.main.activity_info.*
import org.jetbrains.anko.startActivity
import org.jetbrains.anko.toast
import org.koin.androidx.viewmodel.ext.android.getViewModel

class InfoActivity : BaseActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_info)
        initTitle(title = "个人信息修改")
    }

    override fun onStart() {
        super.onStart()
        info_name.setRightString(getString("nickName"))
        info_shop.setRightString(getString("shopName"))
    }

    override fun getViewModel() = getViewModel<SettingViewModel>()

    override fun initTitle(title: String, name: String, divider: Boolean) {
        super.initTitle(title, name, divider)
        info_img.loadCircleImage(getString("headImg").scaleImage())
        info_code.loadImage(getString("shopCode"))

        info_name.oneClick { startActivity<InfoModifyActivity>("type" to "1") }
        info_shop.oneClick { startActivity<InfoModifyActivity>("type" to "2") }

        info_code.oneClick {
            showQRcodeDialog(
                hint = "使用微信扫描进入店铺即可下单",
                type = "url",
                url = getString("shopCode"),
                name = getString("shopName"),
                code = getString("shopId")
            )
        }

        info_img_ll.oneClick {
            PictureSelector.create(this)
                // 全部.PictureMimeType.ofAll()、图片.ofImage()、视频.ofVideo()、音频.ofAudio()
                .openGallery(PictureMimeType.ofImage())
                // 外部传入图片加载引擎，必传项
                .imageEngine(GlideEngine.createGlideEngine())
                // 主题样式设置
                .theme(R.style.Picture_Red_Style)
                // 是否开启微信图片选择风格
                .isWeChatStyle(false)
                // 是否使用自定义相机，仿微信朋友圈
                .isUseCustomCamera(false)
                // 自定义相册启动退出动画
                .setPictureWindowAnimationStyle(PictureWindowAnimationStyle())
                // 列表动画效果
                .setRecyclerAnimationMode(AnimationType.DEFAULT_ANIMATION)
                // 选择数到了最大阀值列表是否启用蒙层效果
                .isMaxSelectEnabledMask(false)
                // 最大图片选择数量
                .maxSelectNum(1)
                // 多选 or 单选
                .selectionMode(PictureConfig.SINGLE)
                // 是否显示拍照按钮
                .isCamera(true)
                // 是否裁剪
                .isEnableCrop(true)
                // 是否压缩
                .isCompress(true)
                // 裁剪比例 如16:9 3:2 3:4 1:1 可自定义
                .withAspectRatio(1, 1)
                // 是否显示uCrop工具栏，默认不显示
                .hideBottomControls(true)
                // 是否显示gif图片
                .isGif(false)
                // 是否圆形裁剪
                .circleDimmedLayer(true)
                // 是否显示裁剪矩形边框 圆形裁剪时建议设为false
                .showCropFrame(false)
                // 是否显示裁剪矩形网格 圆形裁剪时建议设为false
                .showCropGrid(false)
                // 是否开启点击声音
                .isOpenClickSound(false)
                // 裁剪输出质量 默认100
                .cutOutQuality(90)
                // 小于多少kb的图片不压缩
                .minimumCompressSize(100)
                .callback {
                    onResult {
                        OSSHelper.manager.uploadImage(
                            path = it[0].compressPath,
                            onBefore = { showLoading("正在上传中...") },
                            onAfter = { dismissLoading() },
                            onSuccess = { result ->
                                val mUrl = result.responseHeader["url"].orEmpty()
                                getViewModel().editInfo(
                                    "userid" to getString("userId"),
                                    "headimg" to mUrl
                                )
                                    .bindLifeCycle(getLifecycleOwner())
                                    .subscribeByNoError(
                                        event = getViewModel(),
                                        onSuccess = {
                                            toast("图片上传成功！")

                                            putShared("headImg", mUrl)
                                            info_img.loadCircleImage(mUrl.scaleImage())
                                        }
                                    )
                            },
                            OnError = { toast("网络异常，请稍候再试！") }
                        )
                    }
                }
        }
    }

}
